package com.edu.demo.entity;

import com.edu.demo.constant.UserConstant;
import lombok.*;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

/**
 * @ClassName: LoginUser
 * @Description:
 * @Author: jxw
 * @Date: 2023-01-28
 */
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class LoginUser implements Serializable, UserDetails {

    private static final long serialVersionUID = 24559551L;

    /**
     * 登录的用户类型
     */
    private String type;

    private Student student;

    private SysUser sysUser;

    private Teacher teacher;

    private int status;

    private int delete;

    private String password;

    private Set<String> permissions = new HashSet<>();

    @Setter @Getter
    private String token;

    /**
     * 登录时间
     */
    @Setter @Getter
    private Long loginTime;

    /**
     * 过期时间
     */
    @Setter @Getter
    private Long expireTime;


    public Set<String> getPermissions() {
        return permissions;
    }

    public void setPermissions(Set<String> permissions) {
        this.permissions = permissions;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return Collections.emptyList();
    }


    @Override
    public String getPassword() {
        switch (type){
            case UserConstant.ADMIN_TYPE:
                return sysUser.getPassword();

            case UserConstant.STUDENT_TYPE:
                return student.getPassword();
            case UserConstant.TEACHER_TYPE:
                return teacher.getPassword();
        }

        return "";
    }

    @Override
    public String getUsername() {
        switch (type){
            case UserConstant.ADMIN_TYPE:

                return sysUser.getCode();
            case UserConstant.STUDENT_TYPE:

               return student.getCode();
            case UserConstant.TEACHER_TYPE:

                return teacher.getCode();
        }

        return "";
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        status = student.getStatus();
        this.student = student;
    }

    public SysUser getSysUser() {
        return sysUser;
    }

    public void setSysUser(SysUser sysUser) {
        status = sysUser.getStatus();
        this.sysUser = sysUser;
    }

    public Teacher getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher teacher) {
        status = teacher.getStatus();
        this.teacher = teacher;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public int getDelete() {
        return delete;
    }

    public void setDelete(int delete) {
        this.delete = delete;
    }

    /**
     * Indicates whether the user's account has expired. An expired account cannot be
     * authenticated.
     *
     * @return <code>true</code> if the user's account is valid (ie non-expired),
     * <code>false</code> if no longer valid (ie expired)
     */
    @Override
    public boolean isAccountNonExpired() {
        return status==1;
    }

    /**
     * Indicates whether the user is locked or unlocked. A locked user cannot be
     * authenticated.
     *
     * @return <code>true</code> if the user is not locked, <code>false</code> otherwise
     */
    @Override
    public boolean isAccountNonLocked() {
        return status==1;
    }

    /**
     * Indicates whether the user's credentials (password) has expired. Expired
     * credentials prevent authentication.
     *
     * @return <code>true</code> if the user's credentials are valid (ie non-expired),
     * <code>false</code> if no longer valid (ie expired)
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return status == 1;
    }

    /**
     * Indicates whether the user is enabled or disabled. A disabled user cannot be
     * authenticated.
     *
     * @return <code>true</code> if the user is enabled, <code>false</code> otherwise
     */
    @Override
    public boolean isEnabled() {
        return status == 1 ;
    }
}
